package com.jkc.xh.fragments;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jkc.xh.R;
import com.jkc.xh.adapters.AutoListAdapter;
import com.jkc.xh.adapters.core.BaseRecyclerAdapter;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.fragments.core.BasePullRecyclerFragment;
import com.jkc.xh.manager.TraderManager;
import com.jkc.xh.models.AutoModel;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.AutoListResponse;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.views.widget.NavBar;
import com.jkc.xh.views.widget.ToastUtil;
import com.jkc.xh.views.widget.pullview.PullRecyclerView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 策略列表
 */
@EFragment(R.layout.fragment_auto_list)
public class AutoListFragment extends BasePullRecyclerFragment implements BaseRecyclerAdapter.OnViewClickListener {
    @ViewById(R.id.activityTitleName)
    NavBar activityTitleName;

    private AutoListAdapter mAdapter;
    private int pageIndex = 0;
    private int pageTotal = 0;

    @Override
    protected void initView(PullRecyclerView recyclerView) {
        mAdapter = new AutoListAdapter();
        mAdapter.setListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);

        activityTitleName.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onLeftMenuClick(View view) {
                finishFragment();
            }

            @Override
            public void onRightMenuClick(View view) {
                requestData(true);
            }
        });
    }


    @Override
    public void onItemClick(View view, Object object, int position) {
        final AutoModel model = (AutoModel) object;
        switch (view.getId()) {
            case R.id.btnCancel:
                if (model.bstatus == 0) {
                    addJob(TraderManager.revokeAuto(model.auto_id, new RestCallBack<Object>() {
                        @Override
                        public void success(Object o) {
                            ToastUtil.showToastShort(model.stock_name + "撤单成功");
                            requestData(true);
                        }

                        @Override
                        public void failure(RestError error) {

                        }
                    }));
                } else {
                    ToastUtil.showToastShort("无法撤单");
                }

                break;
        }
    }


    private void requestData(final boolean isRefresh) {
        if (isRefresh) {
            pageIndex = 0;
            mAdapter.clear();
            getPullRecyclerView().autoRefresh();
        } else {
            pageIndex++;
            if (pageIndex >= pageTotal) {
                return;
            }
        }
        addJob(TraderManager.getAutoList(pageIndex, new RestCallBack<AutoListResponse>() {
            @Override
            public void success(AutoListResponse response) {
                pageTotal = response.pages;
                if (response.list == null || response.list.size() == 0) {
                    finishLoad(false);
                    return;
                }
                if (isRefresh) {
                    mAdapter.replaceWith(response.list);
                } else {
                    mAdapter.addAll(response.list);
                }
                if (response.list.size() < Constant.PAGE_SIZE) {
                    finishLoad(false);
                } else {
                    finishLoad(true);
                }
            }

            @Override
            public void failure(RestError error) {

            }
        }));
    }


    @Override
    protected void onRefresh(RecyclerView recyclerView) {
        requestData(true);
    }

    @Override
    protected void onLoadMore(RecyclerView recyclerView) {
        requestData(false);
    }

    @AfterViews
    void initData() {
        requestData(true);
    }
}
